package com.zdsoft.blp.zongbuju.wode.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.API.API;
import com.zdsoft.blp.gongyong.fanhui.FanHuiView;
import com.zdsoft.blp.gongyong.net.BaseActivity;
import com.zdsoft.blp.gongyong.net.Net;
import com.zdsoft.blp.gongyong.net.NetCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/9/1.
 */
public class XiuGaiMiMaActivity extends BaseActivity implements NetCallback {
    private FanHuiView back;
    private EditText et_ypassword, et_xpassword, et_cpassword;
    private Button bt_tijiao;
    private SharedPreferences sp;
    private static final Integer XIUGAIPASSWORD = 1;

    @Override
    protected void init() {
        setContentView(R.layout.xiugaimima_wode);
        back = find(R.id.back);
        back.getTv_title().setText("修改密码");

        et_ypassword = find(R.id.et_ypassword);
        et_xpassword = find(R.id.et_xpassword);
        et_cpassword = find(R.id.et_cpassword);
        bt_tijiao = find(R.id.bt_tijiao);
        sp = getSharedPreferences("info", Context.MODE_PRIVATE);

        bt_tijiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String yPassword = et_ypassword.getText().toString().trim();
                String xPassword = et_xpassword.getText().toString().trim();
                String cPassword = et_cpassword.getText().toString().trim();
                if (!xPassword.equals(cPassword)) {
                    Toast.makeText(getApplicationContext(), "两次密码不一致", Toast.LENGTH_SHORT).show();
                } else if (yPassword.equals(xPassword)) {
                    Toast.makeText(getApplicationContext(), "原密码和现密码不能一样", Toast.LENGTH_SHORT).show();
                } else {
                    String userId = sp.getString("userId", "");
                    if (!"".equals(userId)&&userId!="") {
                        Net net = new Net();
                        net.url = API.MEMBER_URL;
                        net.nameSpace = API.MEMBER_NAMESPACE;
                        net.methodName = API.PASSWORDVAL;
                        Map<String, Object> map = new HashMap<>();
                        map.put("userId", userId);
                        map.put("oldpassword", yPassword);
                        map.put("password", xPassword);
                        net.parms = map;
                        net.code = XIUGAIPASSWORD;
                        getServer(net, XiuGaiMiMaActivity.this);
                    }
                }
            }
        });
    }

    @Override
    public void onResponse(boolean flag, int code, String res) {
        if (flag && code == XIUGAIPASSWORD) {
            try {
                JSONObject object = new JSONObject(res);
                Toast.makeText(getApplicationContext(), object.getString("msg"), Toast.LENGTH_SHORT).show();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}
